﻿// EditTextDlg.cpp: 实现文件
//

#include "pch.h"
#include "DuplicateMessage.h"
#include "EditTextDlg.h"
#include "afxdialogex.h"


// EditTextDlg 对话框

IMPLEMENT_DYNAMIC(EditTextDlg, CDialogEx)

EditTextDlg::EditTextDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_EditTextDlg, pParent)
{

}

EditTextDlg::~EditTextDlg()
{
}

EditTextDlg::EditTextDlg(LPVOID lpParameter)
{
	edittextdata = (editTextData*)lpParameter;
	waitInputText = edittextdata->waitInputText;
}
void EditTextDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_WaitPushTextEdit, editText);
	DDX_Control(pDX, IDC_TextList, textList);
}


BEGIN_MESSAGE_MAP(EditTextDlg, CDialogEx)
	ON_BN_CLICKED(IDC_PushText, &EditTextDlg::OnBnClickedPushtext)
	ON_BN_CLICKED(IDC_DeleteText, &EditTextDlg::OnBnClickedDeletetext)
	ON_BN_CLICKED(IDC_CloseWindow, &EditTextDlg::OnBnClickedClosewindow)
END_MESSAGE_MAP()


// EditTextDlg 消息处理程序


void EditTextDlg::OnBnClickedPushtext()
{
	CString tempSave, itemCount;;
	editText.GetWindowTextW(tempSave);
	waitInputText->push_back(tempSave);
	itemCount.Format(_T("%d"), textList.GetItemCount() + 1);
	int row=textList.InsertItem(textList.GetItemCount()+1, itemCount);
	textList.SetItemText(row, 1, tempSave);
	GetDlgItem(IDC_WaitPushTextEdit)->SetWindowTextW(_T(""));
}


void EditTextDlg::OnBnClickedDeletetext()
{
	int nItem = textList.GetItemCount();
	int i = 0;
	std::vector<CString>::iterator iter;
	iter = waitInputText->begin();
	while (i <= nItem+1) {
		if (textList.GetCheck(i) == 1) {
			textList.DeleteItem(i);
			iter = iter + i;
			waitInputText->erase(iter);
			iter = waitInputText->begin();
			i = -1;						//因为后边i++
			nItem = textList.GetItemCount();
		}
		i++;
	}
	//int r = textList.GetSelectionMark();
	//if (textList.GetSelectedCount() != 0)
	//	textList.DeleteItem(r);
}


void EditTextDlg::PostNcDestroy()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::PostNcDestroy();
	delete this;
}


void EditTextDlg::OnCancel()
{
	delete edittextdata;
	DestroyWindow();
}


BOOL EditTextDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	this->SetWindowTextW(_T("文本列表"));
	// TODO:  在此添加额外的初始化

	LONG lStyle;
	lStyle = GetWindowLong(textList.m_hWnd, GWL_STYLE);//获取当前窗口style
	lStyle &= ~LVS_TYPEMASK; //清除显示方式位
	lStyle |= LVS_REPORT; //设置style
	SetWindowLong(textList.m_hWnd, GWL_STYLE, lStyle);//设置style

	DWORD dwStyle = textList.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮（只适用与report风格的listctrl）
	dwStyle |= LVS_EX_GRIDLINES;//网格线（只适用与report风格的listctrl）
	dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
	dwStyle |= LVS_EX_FULLROWSELECT;//选中某行使整行高亮（只适用与report风格的listctrl）
	dwStyle |= LVS_EX_GRIDLINES;//网格线（只适用与report风格的listctrl）
	dwStyle |= LVS_EX_CHECKBOXES;//item前生成checkbox控件
	textList.SetExtendedStyle(dwStyle); //设置扩展风格
	// TODO:  在此添加额外的初始化
	textList.InsertColumn(0, _T("行号"), LVCFMT_CENTER, 60);//插入列
	textList.InsertColumn(1, _T("文本"), LVCFMT_CENTER, 520);//插入列

	CString tempItemCount,tempText;
	std::vector<CString>::iterator iter;
	iter = waitInputText->begin();
	for (int i = 0; i < waitInputText->size();i++) {
		tempItemCount.Format(_T("%d"), i+1);
		int row=textList.InsertItem(i, tempItemCount);
		textList.SetItemText(row, 1, *(iter + i));

	}

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void EditTextDlg::OnBnClickedClosewindow()
{
	OnCancel();
}
